package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.RgService;
import com.bsg.upm.service.SanService;

/**
 * SAN管理控制层
 * 
 * @author HCK
 *
 */
@Auth(value = { UserRoleConstants.ADMIN })
@Controller
@RequestMapping("/v1.0/sans")
public class SanController {

	@Resource
	private SanService sanService;

	@Resource
	private RgService rgService;

	/**
	 * 【SAN查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listSan(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			respJson = sanService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【SAN新增】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveSan(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = sanService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【SAN删除】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeSan(@PathVariable("san") String sanId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = sanService.remove(sanId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【RG查询】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}/rgs", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listRg(@PathVariable("san") String sanId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			searchParamMap.put("sanId", sanId);
			respJson = rgService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【RG新增】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}/rgs", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveRg(@PathVariable("san") String sanId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("sanId", sanId);
			respJson = rgService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【RG启用】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param mgmRgId
	 *            MGM RG编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}/rgs/{rg}/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableRg(@PathVariable("san") String sanId, @PathVariable("rg") String mgmRgId,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = rgService.enable(mgmRgId, true, sanId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【RG停用】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param mgmRgId
	 *            MGM RG编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}/rgs/{rg}/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableRg(@PathVariable("san") String sanId, @PathVariable("rg") String mgmRgId,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = rgService.enable(mgmRgId, false, sanId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【RG删除】
	 * 
	 * @param sanId
	 *            SAN编码
	 * @param mgmRgId
	 *            MGM RG编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{san}/rgs/{rg}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeRG(@PathVariable("san") String sanId, @PathVariable("rg") String mgmRgId,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = rgService.remove(mgmRgId, sanId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
}
